import java.util.Arrays;
import java.util.concurrent.ForkJoinPool;

/**
 * @author BianWenKai
 * @DATE 2021/8/27 - 19:23
 **/
public class Array {

    public static void main(String[] args) {
        int[][][] array = new int[][][]{{{1,2,3},{4,5,6},{7,8,9}},{{9,8,7},{6,5,4},{3,2,1}}};
        System.out.println(array[1][1][1]); // 5

        System.out.println(array.length);  //2
        System.out.println(array[0].length);  // 3
        System.out.println(array[0][0].length);  //3

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                for (int k = 0; k < array[i][j].length; k++) {
                    System.out.print(array[i][j][k] +" ");
                }
                System.out.print("    ");
            }
            System.out.println();
        }


        int[] a = {1,3,8,9,7,2,2,6};
        int[] b = bubbleSort(a);
        int[] c = bubbleSortTwo(a);
        System.out.println(Arrays.toString(b));
        System.out.println(Arrays.toString(c));
        System.out.println();

        System.out.println("======稀疏矩阵======");
        sparseMatrix();

    }


    public static int[] bubbleSort(int[] a){
        for (int i = a.length -1 ; i > 0 ; i--) {
            boolean flag = true;  // 优化，数组是有序的情况下，省略比较操作
            for (int j = 0; j < i; j++) {
                if (a[j] > a[j+1]){
                    int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                    flag = false;
                }
            }
            if (flag)  //当数组是有序的情况下，直接返回即可
                return a;
        }
        return a;
    }


    public static int[] bubbleSortTwo(int[] a){
        for (int i = 0 ; i < a.length ; i++) {
            boolean flag = true;
            for (int j = 0; j < a.length -1 - i; j++) {
                if (a[j] > a[j+1]){
                    int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                    flag = false;
                }
            }
            if (flag)  //当数组是有序的情况下，数组元素将不会产生交换，所以flag也不会发生变化
                return a;
        }
        return a;
    }

    //稀疏矩阵：第一行存放稀疏矩阵的大小和有效元素个数，其余行存放有效元素所在的行列和有效元素的值
    public static void sparseMatrix(){
        int sum = 0;
        int m = 1;
        int[][] array = new int[9][9];
        array[1][2] = 1 ;
        array[2][3] = 2 ;

        for (int[] ints : array) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
        System.out.println("----------------------------------");

        // 1. 统计稀疏数组中有效元素
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (array[i][j] != 0)
                    sum += 1;
            }
        }
        // 2. 将数组中的有效元素放到新数组中
        int[][] newArray = new int[sum+1][3];
        newArray[0][0] = array.length;
        newArray[0][1] = array.length;
        newArray[0][2] = sum;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (array[i][j] != 0){
                    newArray[m][0] = i;
                    newArray[m][1] = j;
                    newArray[m][2] = array[i][j];
                    m++;
                }
            }
        }
        for (int[]  l : newArray){
            for (int n : l) {
                System.out.print(n+"\t");
            }
            System.out.println();
        }
        System.out.println("----------------------------------");
        // 通过稀疏矩阵复原二维数组
        int[][] originalArray = new int[newArray[0][0]][newArray[0][1]];
        for (int i = 1; i < newArray.length; i++) {
            originalArray[newArray[i][0]][newArray[i][1]] = newArray[i][2];
        }
        for (int[] ints : originalArray) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
    }
}
